﻿/*    Copyright 2012 Robert Williams
    
This file is part of DeployFileGenerator.

DeployFileGenerator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

DeployFileGenerator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with DeployFileGenerator.  If not, see <http://www.gnu.org/licenses/>.
*/

// -----------------------------------------------------------------------
// <copyright file="LibDeployFileGenerator.cs" >
// Created by Robert (Bob) Williams, 2012
// </copyright>
//
// CHANGELOG
// -----------------------------------------------------------------------

namespace BW.AppInstaller
{
    using System;
    using System.IO;
    using System.Resources;
    using System.Windows.Forms;

    /// <summary>
    /// Loads files into the Visual Studio Resources folder.
    /// </summary>
    public partial class ApplicationInstaller
    {
        public void CreateResXFile(String sourceFileDirectory, String resXFileName, String xmlFile)
        {
            String resName = String.Empty;
            String resExt = String.Empty;
            String resHash = String.Empty;
            String resFileName = String.Empty;
            UInt64 resFileSize = 0;

            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            String[] files = Directory.GetFiles(sourceFileDirectory);

            #region Create Directories if Necessary
            String xmlFileDir = Path.GetDirectoryName(xmlFile);

            if (! Directory.Exists(xmlFileDir))
                Directory.CreateDirectory(xmlFileDir);

            String resXFileNameDir = Path.GetDirectoryName(resXFileName);

            if (!Directory.Exists(resXFileNameDir))
                Directory.CreateDirectory(resXFileNameDir);
            #endregion Create Directories if Necessary

            try
            {
                using (StreamWriter PackageFiles = File.CreateText(xmlFile))
                {
                    using (ResXResourceWriter resx = new ResXResourceWriter(resXFileName))
                    {
                        PackageFiles.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>");
                        PackageFiles.WriteLine(@"<Files>");

                        foreach (String file in files)
                        {
                            resExt = Path.GetExtension(file).Split('.')[1];
                            resName = Path.GetFileNameWithoutExtension(file) + "_" + resExt;
                            DateTime resFileDt = File.GetLastWriteTime(file);

                            using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
                            {
                                resFileSize = (UInt64)stream.Length;
                                resHash = BitConverter.ToString(sha1.ComputeHash(stream)).Replace("-", String.Empty);
                            }

                            resFileName = Path.GetFileName(file);

                            resx.AddResource(resName, System.IO.File.ReadAllBytes(file));
                            PackageFiles.WriteLine(@"	<File ResName=""{0}"" Hash=""{1}"" Size=""{2}"" DateModified=""{3}"">{4}</File>", resName, resHash, resFileSize, resFileDt, resFileName);
                        }
                        PackageFiles.WriteLine(@"</Files>");
                    }
                }
            }

            catch (FileNotFoundException caught)
            {
                MessageBox.Show("Source: " + caught.Source + " Message: " + caught.Message);
            }
        }
    }
}
